<template>
  <div class="app-container">
    <el-row>
      <!-- 搜索区域 -->
      <el-card style="margin:0 0 5px 5px">
        <el-form ref="queryForm" :inline="true" :model="queryData">
          <el-form-item label="登录名称" prop="Q_loginName_S_LK">
            <el-input v-model="queryData.Q_loginName_S_LK" placeholder="登录名称" />
          </el-form-item>
          <el-form-item class="fr p-r-10">
            <el-button class="filter-item" type="primary" round icon="el-icon-search" @click="handleFilter">
              搜索
            </el-button>
            <el-button class="filter-item m-l-10" type="info" round icon="el-icon-refresh" @click="handleClear">
              清空
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>
      <!-- table区域 -->
      <el-card class="m-l-5">
        <el-button v-permission="['system:user:offline']" v-if="selectionData.length > 0" class="m-b-10 m-l-10" type="danger" icon="el-icon-delete" @click="handleOffline()">
          下线
        </el-button>
        <el-table
          ref="user_table"
          v-loading="tableLoading"
          :data="tableData"
          element-loading-text="加载中"
          border
          fit
          highlight-current-row
          show-overflow-tooltip
          height="100px"
          v-el-height-adaptive-table="{bottomOffset: 110}"
          @sort-change="sortChange"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="40" />
          <el-table-column align="center" label="登录名" prop="loginName" sortable width="100" show-overflow-tooltip />
          <el-table-column label="部门" align="center" prop="deptName" width="120" show-overflow-tooltip />
          <el-table-column label="访问令牌" align="center" prop="accessToken" show-overflow-tooltip />
          <el-table-column label="刷新令牌" align="center" prop="refreshToken" show-overflow-tooltip />
          <el-table-column label="过期时间" align="center" prop="expiresTime" width="180">
            <template slot-scope="scope">
              <span>{{ scope.row.expiresTime | parseTime }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="100" fixed="right">
            <template slot-scope="scope">
              <el-link v-permission="['system:user:offline']" type="danger" icon="el-icon-download" @click="handleOffline(scope.row.id)">下线</el-link>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="tableQuery.total>0" :total="tableQuery.total" :page.sync="tableQuery.page" :limit.sync="tableQuery.limit" @pagination="getTableData" />
      </el-card>
    </el-row>
  </div>
</template>

<script>
import { userOnline, userOffline } from '@/api/system/user'

export default {
  data() {
    return {
      queryData: {
        Q_loginName_S_LK: undefined
      },
      tableLoading: false,
      tableData: [],
      tableQuery: {
        page: 1,
        limit: 20,
        total: 0,
        sort: undefined,
        order: undefined
      },
      selectionData: []
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.getTableData()
    },
    handleFilter() {
      this.tableQuery.page = 1
      this.getTableData()
    },
    handleClear() {
      this.$refs.queryForm.resetFields()
      this.getTableData()
    },
    getTableData() {
      const query = Object.assign(this.tableQuery, this.queryData)
      this.tableLoading = true
      userOnline(query).then(response => {
        this.tableData = response.param.data
        this.tableQuery.total = response.param.recordsTotal
        this.tableLoading = false
      }).catch(e => {
        this.tableLoading = false
      })
    },
    sortChange(data) {
      const { prop, order } = data
      this.tableQuery.sort = prop
      if (order === 'ascending') {
        this.tableQuery.order = 'asc'
      } else {
        this.tableQuery.order = 'desc'
      }
      this.getTableData()
    },
    handleSelectionChange(val) {
      this.selectionData = val
    },
    handleOffline(id) {
      const ids = this.selectionData.map(function(user, index) {
        return user.userId
      }).join(',')

      id = id || ids
      if (!id || id.length === 1) {
        this.$modal.msgWarning('请先选中数据项')
        return
      }

      this.$confirm('是否强制下线所选用户?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        userOffline({
          ids: id
        }).then(response => {
          this.handleFilter()
          this.$modal.msgSuccess('已强制下线' + response.param)
        }).catch(e => {})
      }).catch(e => {})
    }
  }
}
</script>
